﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using OA.Manager;
using OA.Model;
using OA.Utility;

namespace OA.Bll
{
    public class BasePresenter<T> : IBll where T : new()
    {
        public BasePresenter()
        {

        }

        public BasePresenter(ManagerParent<T> manager)
        {
            this.Manager = manager;
        }

        protected ManagerParent<T> Manager = new ManagerParent<T>();

        public virtual string GetSearchAll(JsonReq req)
        {
            string sql = JsonHelper.GetQueryFilterString(req.Options);
            return JsonHelper.ListToJson<T>(Manager.GetSearchAll(sql));
        }

        public virtual string Paged(JsonReq req)
        {
            //int pageIndex = Convert.ToInt32(req["page"]);
            //int pageSize = Convert.ToInt32(req["rows"]);
            //int total = 0;
            //DataTable table = _manager.GetPaged(pageIndex, pageSize, ref total);
            //string result = JsonHelper.OnDataGrid(table, total);
            //return result;
            return Search(req);
        }
        public virtual string Search(JsonReq req)
        {
            int pageIndex = Convert.ToInt32(req["page"]);
            int pageSize = Convert.ToInt32(req["rows"]);
            int total = 0;
            //T info = JsonHelper.JsonDeserialize<T>(req.Options);
            string sql = JsonHelper.GetQueryFilterString(req.Options,false);
            Type type = typeof(T);
            if (type.GetProperties().Count(a => a.Name.ToLower() == "createtime") > 0)
                sql += " order by [createtime] desc";
            DataTable dt = Manager.GetSearchPaged(sql, pageIndex, pageSize, ref total);
            return JsonHelper.OnDataGrid(dt, total);
        }

        public virtual string Add(JsonReq req)
        {
            T info = JsonHelper.JsonDeserialize<T>(req.Options);
            bool bl = Manager.InsertModelInfo(info);
            return JsonHelper.ResultToJson(bl, ExecuteType.Add);
        }

        public virtual string Update(JsonReq req)
        {
            T info = JsonHelper.JsonDeserialize<T>(req.Options);
            List<string> updates = req.Options.Keys.ToList();
            updates.Remove("UserHostAddress");//修改会多一个Ip地址,进行移除.
            bool bl = Manager.UpdateModelInfo(info, updates);//info.ToAttributeList();不使用全部的特性,对于项目的修改区分字段不同,使用特性会有问题.
            return JsonHelper.ResultToJson(bl, ExecuteType.Update);
        }

        public virtual string Delete(JsonReq req)
        {
            T info = new T();
            Type type = info.GetType();
            type.GetProperty("Id").SetValue(info, Convert.ToInt32(req["Id"]), null);
            bool bl = Manager.DeleteModelInfo(info);
            return JsonHelper.ResultToJson(bl, ExecuteType.Delete);
        }
        /// <summary>
        /// 根据ID取得实体并格式化
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public virtual string Get(JsonReq req)
        {
            string result = string.Empty;
            if (req.Options.ContainsKey("ID"))
                result = JsonHelper.JsonSerializer(Manager.GetModelInfoByID(Convert.ToInt16(req.Options["ID"])));
            return result;
        }
        public virtual string IsExit(JsonReq req)
        {
            string sql = JsonHelper.GetQueryFilterString(req.Options, false);
            int total = 0;
            DataTable dt = Manager.GetSearchPaged(sql, 1, 10, ref total);
            bool bl = dt.Rows.Count > 0;
            return JsonHelper.ResultToJson(bl, bl ? "数据已存在" : "数据不存在");
        }
    }
}
